package com.jerry.template.module.mainpage.shouye.duanzi.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.shouye.duanzi.adapter.CommenticonListAdapter;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.CommDetail;
import com.jerry.template.utils.AsyncTask;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

public class ActivityJokeDetailActivity extends BaseActivity {

    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private boolean isClick = true;
    private int isFirst = 1;

    @BindView(R.id.top_joke_back)
    ImageButton top_back;
    @BindView(R.id.user_head)
    CircleImageView civ_userHead;
    @BindView(R.id.user_name)
    TextView tv_userName;
    @BindView(R.id.content)
    TextView tv_duanziContent;
    @BindView(R.id.duanzi_Tag)
    Button btn_duanziTag;
    @BindView(R.id.btn_digg)
    Button btn_duanziDigg;
    @BindView(R.id.btn_bury)
    Button btn_duanziBury;
    @BindView(R.id.btn_commenticon)
    Button btn_duanziCommenticon;
    @BindView(R.id.btn_joke_share)
    Button ib_duanziShare;
    @BindView(R.id.lv_commenticon)
    PullToRefreshListView lv_commenticon;

    private int duanziDigg;
    private CommenticonListAdapter mAdapter = null;
    private List<CommDetail.DataBean.RecentCommentsBean> mDataBean = new ArrayList<CommDetail.DataBean.RecentCommentsBean>();
    private String groupId;
    private String comm_url;
    private boolean isFav;

    /**
     * 绑定layout
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_joke_detail;
    }

    /**
     * 初始化视图
     */
    @Override
    protected void initViews() {

        //获取到点击的item传递过来的详细内容
        Intent intent = getIntent();
        String userName = intent.getStringExtra("userName");
        String duanziContent = intent.getStringExtra("duanziContent");
        duanziDigg = intent.getIntExtra("duanziDigg", 1);
        String duanziBury = intent.getStringExtra("duanziBury");
        String duanziCommenticon = intent.getStringExtra("duanziCommenticon");
        String userHead = intent.getStringExtra("userHead");
        groupId = intent.getLongExtra("groupId", 2) + "";
        isFav = intent.getBooleanExtra("isFav", true);

        //控件初始化
        tv_userName.setText(userName);
        tv_duanziContent.setText(duanziContent);
        btn_duanziDigg.setText(duanziDigg + "");
        btn_duanziBury.setText(duanziBury);
        btn_duanziCommenticon.setText(duanziCommenticon);

        //加载用户头像
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
        imageLoader.displayImage(
                userHead,
                civ_userHead,
                options);

        //点赞功能的实现
        Drawable drawable = null;

        if (isFav) {
            drawable = this.getResources().getDrawable(R.drawable.ic_digg_pressed);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            btn_duanziDigg.setCompoundDrawables(drawable, null, null, null);
            duanziDigg += 1;
            btn_duanziDigg.setText(duanziDigg + "");
        } else {
            btn_duanziDigg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isFirst += 1;
                    Drawable drawable = null;
                    if (isFirst == 2) {
                        if (isFav) {
                            drawable = v.getResources().getDrawable(R.drawable.ic_digg_pressed);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            btn_duanziDigg.setCompoundDrawables(drawable, null, null, null);
                            duanziDigg += 1;
                            btn_duanziDigg.setText(duanziDigg + "");
                        } else {
                            drawable = v.getResources().getDrawable(R.drawable.ic_digg_normal);
                            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                            btn_duanziDigg.setCompoundDrawables(drawable, null, null, null);
                        }
                    }

                    if (isFirst >= 3) {
                        Toast.makeText(ActivityJokeDetailActivity.this, "你已经顶过了", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

        //初始化适配器
        mAdapter = new CommenticonListAdapter(this, mDataBean);

        lv_commenticon.setAdapter(mAdapter);

        //列表添加下拉刷行的实现
        lv_commenticon.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData();
                mAdapter.notifyDataSetChanged();
                new FinishRefresh().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        //返回键的实现
        top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityJokeDetailActivity.this.finish();
            }
        });
    }

    /**
     * 下拉刷新需要进行的异步任务
     */
    private class FinishRefresh extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            lv_commenticon.onRefreshComplete();
        }
    }

    /**
     * OkHttp实现的加载网络数据
     */
    @Override
    protected void loadData() {
        comm_url = Urls.COMM_LIST_TOP + groupId + com.jerry.template.constant.Urls.COMM_LIST_END;
        OkHttpUtils.postSubmitForm(comm_url, null);
    }

    /**
     * JSON解析并传递给适配器处理
     */
    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        super.postEventResult(message);
        if (message != null) {
            if (message.type.equals(comm_url)) {
                String result = message.result;
                Gson gson = new Gson();
                CommDetail commDetail = gson.fromJson(result, CommDetail.class);
                mDataBean.addAll(commDetail.getData().getRecent_comments());
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}
